﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IETTortak
{
    public class ServiceTranslator
    {
        private static int SabahaNormalize(int time)
        {
            return (time - 6 * 60 + 23 * 60 + 59) % (23 * 60 + 59);
        }


        public static string GetCompactHours(Service s, DateTime dt)
        {
            return GetCompactHours(s, dt, 0);
        }

        public static string GetCompactHours(Service s, ServiceTime.DayTypeEnum dayType)
        {
            return GetCompactHours(s, dayType, 0);
        }

        public static string GetCompactHours(Service s, ServiceTime.DayTypeEnum dayType, int minuteRange)
        {
            int timeNow = DateTime.Now.Hour * 60 + DateTime.Now.Minute;

            int timeLow = (timeNow - minuteRange + 23 * 60 + 59) % (23 * 60 + 59);
            int timeHi = (timeNow + minuteRange + 23 * 60 + 59) % (23 * 60 + 59);

            var hatGidiş = s.ServiceTimes.FindAll(
                x =>
                    x.DayType == dayType
                    && x.Returning == ServiceTime.ReturningEnum.Gidiş
                    && (minuteRange == 0 || (x.Time >= timeLow && x.Time <= timeHi))
                    );

            var hatDönüş = s.ServiceTimes.FindAll(
                x =>
                    x.DayType == dayType
                    && x.Returning == ServiceTime.ReturningEnum.Dönüş
                    && (minuteRange == 0 || (x.Time >= timeLow && x.Time <= timeHi))
                    );

            // sabah 6ya normalize et
            //(@time-@timeMorning+23*60+59) % (23*60+59)

            hatGidiş.Sort((x, y) => SabahaNormalize(x.Time).CompareTo(SabahaNormalize(y.Time)));
            hatDönüş.Sort((x, y) => SabahaNormalize(x.Time).CompareTo(SabahaNormalize(y.Time)));


            StringBuilder sb = new StringBuilder();

            sb.Append(s.ServiceCode.Replace('|', '/'));
            sb.Append("|");

            sb.Append(s.Notlar.Replace("|", "/"));
            sb.Append("|");

            string kalkis = DAL.ExecScalar("SELECT ServiceInfo.ufnGetKalkis(@RouteId)", false, "@RouteId", s.RouteID).ToString();
            sb.Append(kalkis.Replace('|', '/'));
            sb.Append("|");

            sb.Append(hatGidiş.Count);
            sb.Append("|");

            foreach (ServiceTime st in hatGidiş)
            {
                sb.Append(st.ToCompactString());
                sb.Append("|");
            }

            string varis = DAL.ExecScalar("SELECT ServiceInfo.ufnGetVaris(@RouteId)", false, "@RouteId", s.RouteID).ToString();
            sb.Append(varis.Replace('|', '/'));
            sb.Append("|");

            sb.Append(hatDönüş.Count);
            sb.Append("|");

            foreach (ServiceTime st in hatDönüş)
            {
                sb.Append(st.ToCompactString());
                sb.Append("|");
            }

            //return Translate(sb.ToString());
            return sb.ToString();
        }

        public static string GetCompactHours(Service s, DateTime dt, int minuteRange)
        {
            ServiceTime.DayTypeEnum dayType;
            switch (dt.DayOfWeek)
            {
                case DayOfWeek.Saturday:
                    dayType = ServiceTime.DayTypeEnum.Cumartesi;
                    break;
                case DayOfWeek.Sunday:
                    dayType = ServiceTime.DayTypeEnum.PazarTatil;
                    break;
                default:
                    dayType = ServiceTime.DayTypeEnum.İşgünü;
                    break;
            }

            return GetCompactHours(s, dayType, minuteRange);

        }
    }
}
